package com.eCommerce.server.sisv.impl;

import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.eCommerce.corecontract.criteria.CategorieCriteria;
import com.eCommerce.corecontract.criteria.ComposantCriteria;
import com.eCommerce.corecontract.dto.CategorieDTO;
import com.eCommerce.corecontract.dto.ComposantDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.corecontract.exception.EcommerceSISVException;
import com.eCommerce.server.corebackend.dao.DAOController;
import com.eCommerce.server.corebackend.dao.ICategorieDAO;
import com.eCommerce.server.corebackend.dao.IComposantDAO;
import com.eCommerce.server.corebackend.sisv.ICategorieSISV;
import com.eCommerce.server.corebackend.util.BackEndConstants;
import com.eCommerce.server.dao.impl.CategorieDAOImpl;




public class CategorieSISVImpl extends AbstractCommonSISV implements ICategorieSISV{
	
	
	private static Logger log =Logger.getLogger(CategorieSISVImpl.class);
	private DAOController _daoController = null;
	private ICategorieDAO _categorieDaoImpl = null;
	private IComposantDAO _composantDAOImpl=null;
	
	
	public CategorieSISVImpl(DAOController daoController) throws Exception {
		_daoController = daoController;
		_categorieDaoImpl = (ICategorieDAO) _daoController.get(BackEndConstants.DAO_CATEGORIE);
		_composantDAOImpl = (IComposantDAO) _daoController.get(BackEndConstants.DAO_COMPOSANT);
		
	}


	public Long ajouterCategorie(CategorieDTO categorieDTO)
			throws EcommerceException {
		try {
			 _categorieDaoImpl.insert(categorieDTO);
			 categorieDTO=_categorieDaoImpl.findOne(categorieDTO);
			 categorieDTO.setCode("FAMILLE/"+categorieDTO.getId().toString());
			 _categorieDaoImpl.update(categorieDTO);
			 return categorieDTO.getId();
		}catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		} catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCATEGORIE-001", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}


	public void modifierrCategorie(CategorieDTO categorieDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			 _categorieDaoImpl.update(categorieDTO);//insert(categorieDTO);
		}catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		} catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCATEGORIE-002", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
	}

	
	public 	List rechercherCategorie(CategorieCriteria categorieCriteria)
			throws EcommerceException {
		try {
			return _categorieDaoImpl.find(categorieCriteria);//.insert(categorieDTO);
		}catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		} catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCATEGORIE-004", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

	public void supprimerCategorie(CategorieDTO categorieDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			
		
			
			List listOfComposantDTO= _composantDAOImpl.findComposantByIdCategorie(categorieDTO);//.find(composantCriteria);
			if (listOfComposantDTO!=null && listOfComposantDTO.size()!=0) 
				{
				Object[] arg = new Object[1];
				arg[0]=categorieDTO.getLibelle();
				EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCATEGORIE-006", arg);
				log.error(sisvException.getMessage());
				throw sisvException;
				}
			else 
			{
				_categorieDaoImpl.delete(categorieDTO);
			}
		} 
		catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		}
		catch (Exception e) {
			Object[] arg = new Object[1];
			arg[0]=categorieDTO.getLibelle();
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCATEGORIE-005", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
		
	}




	public boolean validerCategorie(CategorieDTO categorieDTO)
			throws EcommerceException {
		// TODO Auto-generated method stub
		return false;
	}

	
	public CategorieDTO visualiserCategorie(CategorieDTO categorieDTO)
			throws EcommerceException {
		try {
			return (CategorieDTO) _categorieDaoImpl.findOne(categorieDTO);//(categorieCriteria);//.insert(categorieDTO);
		}catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		} catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCATEGORIE-003", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}


	public List listerCategorie() throws EcommerceException {
		// TODO Auto-generated method stub
		try {
			return _categorieDaoImpl.findAll();//.find(categorieCriteria);//.insert(categorieDTO);
		}catch (EcommerceException eCommerceException) {
			throw eCommerceException;
		} catch (Exception e) {
			Object[] arg = new Object[1];
			EcommerceSISVException sisvException = new EcommerceSISVException(messagesXmlFileName, "ECOMMERCESISVCATEGORIE-004", arg, e.getMessage());
			log.error(sisvException.getMessage());
			throw sisvException;
		}
	}

}
